package com.numbfish.ch81_ex02_selenium_official_start.test;

import cn.hutool.core.io.file.FileReader;
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import org.junit.jupiter.api.Test;
import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSStyleSheet;

import java.io.*;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class CssParserUtils {
    @Test
    public void result() throws IOException {
        String s = generateJqueryCssFromFile("js/renderCss.css");
        System.out.println(s);
    }


    public static String generateJqueryCssFromFile(String path) {
        StringBuilder sb = new StringBuilder();
        String cssTxtString = new FileReader(path).readString();
        InputSource source = new InputSource(new StringReader(cssTxtString));
        CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
        CSSStyleSheet sheet = null;

        try {
            sheet = parser.parseStyleSheet(source, null, null);
        } catch (IOException e) {
            System.out.println("catch到异常");
            throw new RuntimeException(e);
        }
        CSSRuleList rules = sheet.getCssRules();
        for (int i = 0; i < rules.getLength(); i++) {
            final CSSRule rule = rules.item(i);
            Map.Entry<String, String> selectorAndStyle = getSelectorAndStyle(rule.getCssText());
            String selectorString = selectorAndStyle.getKey();
            String cssContent = selectorAndStyle.getValue().trim();
            String singleElementCss = null;
            singleElementCss = "$(" +"'"+ selectorString +"'"+ ")" + ".css(" + concatCssToString(cssContent) + ");\n";
            sb.append(singleElementCss);
        }
        return sb.toString();
    }

    //$("p").css({"background-color":"yellow","font-size":"200%"});
    public static String concatCssToString(String line) {
        StringBuilder sb = new StringBuilder();
        InputSource source = new InputSource(new StringReader(line));
        CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
        CSSStyleDeclaration decl = null;
        try {
            decl = parser.parseStyleDeclaration(source);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        sb.append("{");
        for (int i = 0; i < decl.getLength(); i++) {
            final String propName = decl.item(i);
            sb.append("'" + propName + "'" + ":" + "'" + decl.getPropertyValue(propName) + "'" + ",");
            // System.out.println("'" + propName + "' has value: '" + decl.getPropertyValue(propName) + "'");
        }
        if (sb.length() > 0) {
            // 删除最后一个逗号
            sb.setLength(sb.length() - 1);
        }
        sb.append("}");
        return sb.toString();
    }


    public static Map.Entry<String, String> getSelectorAndStyle(String line) {
        // 按指定模式在字符串查找
        String pattern = "(.*)\\{(.*)\\}";

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        if (m.find()) {
            Map.Entry<String, String> pair = Map.entry(m.group(1), m.group(2));
            // System.out.println("Found value: " + m.group(1) );
            // System.out.println("Found value: " + m.group(2) );
            return pair;

        } else {
            System.out.println("NO MATCH");
        }
        return null;
    }

}

